$(function () {
    var schoolSelector = "#School";
    //province;
    //proSchool;
    //学校名称 激活状态
    $(schoolSelector).click(function () {
        var top = $(this).position().top + $(this).height() + 2;
        var left = $(this).position().left;
        $("div[class='provinceSchool']").css({ top: top, left: left });
        $("div[class='provinceSchool']").show();
    });
    //初始化省下拉框
    var provinceArray = "";
    var provicneSelectStr = "";
    for (var i = 0, len = province.length; i < len; i++) {
        provinceArrayText = province[i].Text;
        provinceArrayId = province[i].Value;
        provicneSelectStr = provicneSelectStr + "<option Value='" + provinceArrayId + "'>" + provinceArrayText + "</option>"
    }
    $("div[class='proSelect'] select").html(provicneSelectStr);


    function doSchoolList(data) { //输出列表
        var schoolUlStr = "";
        var schoolListArray = data;
        var tempSchoolName = "";
        for (var i = 0, len = schoolListArray.length; i < len; i++) {
            tempSchoolName = schoolListArray[i].Text;
            tempSchoolId = schoolListArray[i].Value;
            if (tempSchoolName.length > 13) {
                schoolUlStr = schoolUlStr + "<li val=" + tempSchoolId + " class='DoubleWidthLi' title=" + tempSchoolName + ">" + tempSchoolName + "</li>"
            } else {
                schoolUlStr = schoolUlStr + "<li val=" + tempSchoolId + " >" + tempSchoolName + "</li>"
            }
        }
        return schoolUlStr;
    }
    //初始化学校列表
    $.post('/Common/GetUniversities', { provinceId: province[0].Value }, function (data, TextStatus, xhr) {
            /*optional stuff to do after success */
            $("div[class='schoolList'] ul").html(doSchoolList(data));
        });
    
    //省切换事件
    $("div[class='proSelect'] select").change(function() {
        var _thisVal = $(this).val()
        $.post('/Common/GetUniversities', { provinceId: _thisVal }, function(data, TextStatus, xhr) {
            /*optional stuff to do after success */
            $("div[class='proSelect'] span").show();
            $("div[class='proSelect'] input").hide();
            $("div[class='schoolList'] ul").html(doSchoolList(data));
        });
    });
    //学校列表mouseover事件
    $(document).on("mouseover", "div[class='schoolList'] ul li", function () {
        $(this).css("background-color", "#72B9D7");
    });
    //学校列表mouseout事件
    $(document).on("mouseout", "div[class='schoolList'] ul li", function () {
        $(this).css("background-color", "");
    });
    //学校列表点击事件
    $(document).on("click", "div[class='schoolList'] ul li", function () {
        $(schoolSelector).val($(this).html());
        if ($(schoolSelector + "_Id").length) {
            $(schoolSelector + "_Id").val($(this).attr("val"));//设置id的input
        }
        $("div[class='provinceSchool']").hide();
    });
    //按钮点击事件
    $(document).on("click", "div[class='button'] a", function () {
        var flag = $(this).attr("flag");
        if ("0" == flag) {
            $("div[class='provinceSchool']").hide();
        } else if ("1" == flag) {
            var selectPro = $("div[class='proSelect'] select").val();
            if ("99" == selectPro) {
                $(schoolSelector).val($("div[class='proSelect'] input").val());
            }
            $("div[class='provinceSchool']").hide();
        }
    });
});
